// myself/apply/BusApply.js
import api from "../../utils/api.js";
import utils from "../../utils/utils.js";
import network from "../../utils/network.js";
import formatDate from "../../utils/format_date.js";
const app = getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {
    finish: true,
    starttime:'',
    starttime1:'',
    endtime:'',
    endtime1:'',
    shownext:false,
    nextUsers:[],
    dataNextUser:[],
    sysnextopt:'',
    sysnextoptid:'',
    setid:106
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.toast = this.selectComponent("#toast");
    network.postJson(this, api.getNext, {
      adminid: app.globalData.userInfo.uid,
      token: app.globalData.userInfo.token,
      setid: this.data.setid,
    }).then(res => {
      console.log(res.data.data)
      if(res.data.data.shownext){
        let arr = [];
        var result = res.data.data.nextUser;
        for (const key in result) {
          arr.push({ id: result[key].id, username: result[key].name+" ("+result[key].ranking+")" });
        }
        this.setData({
          nextUsers:arr,
          dataNextUser:res.data.data.nextUser,
          nextName:res.data.data.nextName
        })
        
      }
      this.setData({
        shownext: res.data.data.shownext,
        sysnextcustidid:res.data.data.sysnextcustidid
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  bindNextChange: function (e) {
    let applyIndex = e.detail.value
    let nextUserList = this.data.nextUsers
    let dataNextUser = this.data.dataNextUser
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      applyIndex:applyIndex,
      sysnextoptid:nextUserList[applyIndex].id,
      sysnextopt:dataNextUser[applyIndex].name
    })
  },
  bindDateChange1: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      starttime: e.detail.value
    })
  },
  bindDateChange2: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      starttime1: e.detail.value
    })
  },
  bindDateChange3: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      endtime: e.detail.value
    })
  },
  bindDateChange4: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      endtime1: e.detail.value
    })
  },

  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    if (!e.detail.value.purpose) {
      this.toast.showToast('请写明用途');
      return;
    } else if (!this.data.starttime) {
      this.toast.showToast('请选择开始时间');
      return;
    }else if (!this.data.starttime1) {
      this.toast.showToast('请选择开始时间');
      return;
    }else if (!this.data.endtime) {
      this.toast.showToast('请选择结束时间');
      return;
    }else if (!this.data.endtime1) {
      this.toast.showToast('请选择结束时间');
      return;
    }else if (!e.detail.value.address) {
      this.toast.showToast('请选填写目的地');
      return;
    }else if (!e.detail.value.number) {
      this.toast.showToast('请选填写用车人数');
      return;
    }else if (!e.detail.value.user) {
      this.toast.showToast('请选填写用车人');
      return;
    }else if (!e.detail.value.phone) {
      this.toast.showToast('请选填写手机号');
      return;
    }
    let startTime = this.data.starttime  + ' ' + this.data.starttime1
    let endTime = this.data.endtime + ' ' + this.data.endtime1
    if(startTime >= endTime){
      this.toast.showToast('结束时间不能早于开始时间');
      return;
    }
    console.log(startTime)
    if(this.data.shownext && (!this.data.sysnextoptid || !this.data.sysnextopt)){
      this.toast.showToast('请选择经办人员');
      return;
    }
    if(this.data.shownext && this.data.sysnextcustidid == 0){
      this.toast.showToast('系统错误');
      return;
    }


    network.postJson(this, api.save, {
      adminid: app.globalData.userInfo.uid,
      token: app.globalData.userInfo.token,
      id: 0,
      purpose: e.detail.value.purpose,
      day: this.data.day,
      starttime: startTime,
      endtime:endTime,
      address:e.detail.value.address,
      number:e.detail.value.number,
      user:e.detail.value.user,
      phone:e.detail.value.phone,
      sysmodenum: 'companycar',
      sysmodeid: this.data.setid,
      sysnextcustidid:this.data.sysnextcustidid,
      sysnextoptid:this.data.sysnextoptid,
      sysnextopt:this.data.sysnextopt
    }).then(res => {
      this.toast.showToast('提交成功');
      wx.navigateBack();
    });
  }
})